package com.dmgn.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.dmgn.bean.RequestParam;
import com.dmgn.bean.ResponseParam;
import com.dmgn.bean.User;
import com.dmgn.service.ICSVConverter;
import com.dmgn.service.IUserService;
import com.dmgn.serviceimpl.CSVImportUserImpl;
import com.dmgn.serviceimpl.UserServiceImpl;
import com.dmgn.util.BaseAction;
import com.dmgn.util.ChangeFileName;
import com.dmgn.util.MD5;
import com.dmgn.util.Setting;
import com.dmgn.util.UploadFile;

public class UserAction extends BaseAction{
	
	/**
	 * @Fields serialVersionUID : TODO
	 */
	private static final long serialVersionUID = 1L;
	
	private User user;
	private RequestParam requestParam;
	private ResponseParam responseParam;
	
	private List<User> userList = new ArrayList<User>();
	
	private IUserService userService;
		
	public UserAction(){
		userService = new UserServiceImpl();
	}
	
	/**
	 * 用户登录
	 * param：user
	 * 
	 * @return
	 */
	public String userLogin(){
		MD5 md=new MD5();
		user.setPassword(md.MD5(user.getEmployeenumber()+user.getPassword()));
		user = userService.findSingleUser(user);
		if(user!=null){
			if (!user.getStatus().toString().equals("-1")){
				session.put("userId",user.getId());
				session.put("role",user.getRole());
				session.put("employeenumber",user.getEmployeenumber());
				return SUCCESS;
			}
			else{
				responseParam.setMessage("该用户已被禁用");
				return ERROR;
			}
		}
		else{
			responseParam.setMessage("用户名或密码错误");
			return ERROR;		
		}
	}

	/**
	 * 获取用户
	 * param：requestParam
	 * 
	 * @return
	 */
	public String getUserArray(){
		user = new User();
		userList = userService.findUser(requestParam.getPageNo(),requestParam.getQueryStr(),user);
		responseParam = new ResponseParam();
		responseParam.setTotalNum(userService.getTotalNum(requestParam.getQueryStr(), user));
		return SUCCESS;
	}
	
	/**
	 * 获取用户信息
	 * param：user
	 * 
	 * @return
	 */
	public String getUserInfo(){
		int userId = (Integer) session.get("userId");
		user.setId(userId);
		user = userService.findSingleUser(user);
		responseParam = new ResponseParam();
		if(user!=null){
			if (!user.getStatus().toString().equals("-1")){
				return SUCCESS;
			}
			else{
				responseParam.setMessage("用户已被禁用");
				return ERROR;
			}
		}
		else{
			responseParam.setMessage("获取用户信息失败");
			return ERROR;
		}		
	}
	
	/**
	 * 修改用户信息
	 * param：user
	 * 
	 * @return
	 */
	public String changeUserInfo(){
		int userId = (Integer) session.get("userId");
		user.setId(userId);

		responseParam = new ResponseParam();
		if(userService.updateUser(user)){
			responseParam.setMessage("修改成功");
			return SUCCESS;
		}
		else{
			responseParam.setMessage("修改失败");
			return ERROR;
		}
	}
	
	
	/**
	 * 用逐个添加用户
	 * param：user
	 * 
	 * @return
	 */
	public String addUser(){
		userService.addUser(user);
		responseParam = new ResponseParam();
		MD5 md=new MD5();
		user.setPassword(md.MD5((String)user.getEmployeenumber() + Setting.resetPassword));
		
		if(userService.addUser(user)){
			responseParam.setMessage("添加用户成功");
			return SUCCESS;
		}
		else{
			responseParam.setMessage("添加用户失败");
			return ERROR;		
		}
	}

	/**
	 * 批量导入用户
	 * param：requestParam
	 * 
	 * @return
	 */
	public String batchImportUser(){
		String fileName = "";
		ChangeFileName cf = new ChangeFileName();
		UploadFile uf = new UploadFile();
		
		String excelFileName = requestParam.getExcelFileName();
		String excelContentType = requestParam.getExcelContentType();
		File excel = requestParam.getExcel();
		
		excelFileName="\\"+cf.changeFileName(excelFileName);
		fileName = uf.upload(excel, excelFileName);
		
		ICSVConverter csv = null;
		
		csv = new CSVImportUserImpl();
		
		
		String result="";
		
		result= csv.batchImport(fileName);
		
		if(result != ""){
			responseParam.setMessage(result);
		}
		return SUCCESS;
	}
	
	/**
	 * 修改密码
	 * param：requestParam
	 * 
	 * @return
	 */
	public String changePass() {
		
		responseParam = new ResponseParam(); 
		
		if(requestParam!=null){
			if(requestParam.getPassword().toString().equals(requestParam.getNewPassword().toString())){
				user.setId((Integer) session.get("userId"));
				MD5 md=new MD5();
				user.setPassword(md.MD5((String)session.get("employeenumber") +user.getPassword()));
				user = userService.findSingleUser(user);
				if(user!=null){
					user.setPassword(md.MD5((String)session.get("employeenumber")+requestParam.getPassword()));
					if(userService.updateUser(user)){
					responseParam.setMessage("修改密码成功");
					return SUCCESS;
					}
					else{
						responseParam.setMessage("修改密码失败");
						return ERROR;
					}
				}
				else{
					responseParam.setMessage("密码错误");
					return ERROR;
				}
			}
			else{
				responseParam.setMessage("两次密码不相同");
				return ERROR;
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 密码批量重置
	 * param：requestParam
	 * @return
	 */
	public String resetPass() {
		responseParam = new ResponseParam(); 
		if(requestParam!=null){
			int userId = (Integer) session.get("userId");
			String employeenumber = (String) session.get("employeenumber");
			user.setId(userId);
			user.setEmployeenumber(employeenumber);
			MD5 md=new MD5();
			user.setPassword(md.MD5((String)user.getEmployeenumber() + requestParam.getPassword()));
			user = userService.findSingleUser(user);
			if(user!=null){
				String[] userIdStr = requestParam.getUserIdStr().split(",");
				String message="职工号";
				for(int i=0; i<userIdStr.length;i++){
					user = new User();
					user.setEmployeenumber(userIdStr[i]);
					user.setPassword(md.MD5((String)user.getEmployeenumber() + Setting.resetPassword));
					System.out.println(user.getPassword());
					if(!userService.updatePassword(user)){
						message += user.getEmployeenumber();
					}
				}
				if(message.equals("职工号")){
					responseParam.setMessage("重置密码成功");
					return SUCCESS;
				}
				else{
					message += "重置密码失败";
					responseParam.setMessage(message);
					return ERROR;
				}
			}else{
				responseParam.setMessage("密码不正确");
				return ERROR;
			}
		}else{
			responseParam.setMessage("参数错误。");
			return ERROR;
		}
		
	}
	
	/**
	 * 修改用户权限
	 * param：user
	 * 
	 * @return
	 */
	public String changeUser(){
		user = new User();
		user.setId((Integer) session.get("userId"));
		MD5 md=new MD5();
		user.setPassword(md.MD5((String)session.get("employeenumber") +requestParam.getPassword()));
		user = userService.findSingleUser(user);
		responseParam = new ResponseParam();
		if(user!=null){
			if(userService.updateRole(requestParam.getUserIdStr(), requestParam.getRole())){
				responseParam.setMessage("修改权限成功");
				return SUCCESS;
			}
			else{
				responseParam.setMessage("修改权限失败");
				return ERROR;
			}
		}
		else
		{
			responseParam.setMessage("密码错误");
			return ERROR;
		}
			
	}
	
	/**
	 * 删除用户
	 * param：requestParam
	 * 
	 * @return
	 */
	public String deleteUser(){
		user = new User();
		user.setId((Integer) session.get("userId"));
		MD5 md=new MD5();
		user.setPassword(md.MD5((String)session.get("employeenumber") +requestParam.getPassword()));
		user = userService.findSingleUser(user);
		responseParam = new ResponseParam();
		if(user!=null){
			if(userService.updateStatus(requestParam.getUserIdStr(), -1)){
				responseParam.setMessage("删除成功");
				return SUCCESS;
			}
			else{
				responseParam.setMessage("删除失败");
				return ERROR;
			}
		}
		else{
			responseParam.setMessage("密码错误");
			return ERROR;
		}
	}
	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public RequestParam getRequestParam() {
		return requestParam;
	}

	public void setRequestParam(RequestParam requestParam) {
		this.requestParam = requestParam;
	}

	public ResponseParam getResponseParam() {
		return responseParam;
	}

	public void setResponseParam(ResponseParam responseParam) {
		this.responseParam = responseParam;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	
}
